package com.frame.system.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.frame.common.core.entity.SysDictData;
import com.frame.common.core.security.utils.SecurityUtils;
import com.frame.db.util.Condition;
import com.frame.db.util.Query;
import com.frame.stant.utils.R;
import com.frame.system.service.ISysDictDataService;
import com.frame.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@Api(tags = "字典数据")
@RestController
@RequestMapping("/system/dict/data")
@AllArgsConstructor
public class SysDictDataController {

    private final ISysDictDataService dictDataService;
    private final ISysDictTypeService dictTypeService;

    @PreAuthorize("@custom.hasAnyAuthority('system:dict:list')")
    @GetMapping("/list")
    public R list(SysDictData dictData, Query query) {
        IPage<SysDictData> list = dictDataService.selectDictDataList(Condition.page(query),dictData);
        return R.success(list);
    }

//    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
//    @PreAuthorize("@custom.hasAnyAuthority('system:dict:export')")
//    @GetMapping("/export")
//    public AjaxResult export(SysDictData dictData) {
//        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
//        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
//        return util.exportExcel(list, "字典数据");
//    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public R getInfo(@PathVariable Long dictCode) {
        return R.success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public R dictType(@PathVariable String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (CollectionUtil.isEmpty(data)) {
            data = new ArrayList<>();
        }
        return R.success(data);
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:dict:add')")
//    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@Validated @RequestBody SysDictData dict) {
        dict.setCreateBy(SecurityUtils.getUsername());
        return R.success(dictDataService.insertDictData(dict) > 0);
    }

    /**
     * 修改保存字典类型
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:dict:edit')")
//    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@Validated @RequestBody SysDictData dict) {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return R.success(dictDataService.updateDictData(dict) > 0);
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@custom.hasAnyAuthority('system:dict:remove')")
//    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public R remove(@PathVariable Long[] dictCodes) {
        return R.success(dictDataService.deleteDictDataByIds(dictCodes) > 0);
    }
}
